﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using uTools;
public class LevelCurTime : MonoBehaviour
{
    public TextButton h1;
    public TextButton h2;
    public TextButton m1;
    public TextButton m2;
    public uTweenAlpha tweenAlpha;

    public uTweenPosition tweenPosLeft;
    public uTweenPosition tweenPosRight;
    public GameLevelItem gameLevelItem;

    private int hour1 = 0;
    private int hour2 = 0;
    private int min1 = 0;
    private int min2 = 0;
    private bool isLevelFinish = false;

    void Start()
    {
        InitBtnEvent();
    }

    private void InitBtnEvent()
    {
        h1.Init(hour1.ToString(), () =>
        {
            if (isLevelFinish)
                return; 
            hour1++;
            if (hour1 > 2)
                hour1 = 0;
            h1.text.text = hour1.ToString();
            JudgeResult();

        });
        h2.Init(hour2.ToString(), () =>
        {
            if (isLevelFinish)
                return;
            hour2++;
            if (hour2 > 9)
                hour2 = 0;
            h2.text.text = hour2.ToString();
            JudgeResult();
        });
        m1.Init(min1.ToString(), () =>
        {
            if (isLevelFinish)
                return;
            min1++;
            if (min1 > 6)
                min1 = 0;
            m1.text.text = min1.ToString();
            JudgeResult();
        });
        m2.Init(min2.ToString(), () =>
        {
            if (isLevelFinish)
                return;
            min2++;
            if (min2 > 9)
                min2 = 0;
            m2.text.text = min2.ToString();
            JudgeResult();
        });
    }

    private void JudgeResult()
    {
        string hourStr = h1.text.text + h2.text.text;
        string minStr = m1.text.text + m2.text.text;
        string nowHourStr = string.Empty;
        string nowMinStr = string.Empty;
        int hour = System.DateTime.Now.Hour;
        int min = System.DateTime.Now.Minute;
        if (hour < 10)
            nowHourStr = string.Format("0{0}", hour);
        else
            nowHourStr = hour.ToString();
        if (min < 10)
            nowMinStr = string.Format("0{0}", min);
        else
            nowMinStr = min.ToString();
        if (hourStr.Equals(nowHourStr) && minStr.Equals(nowMinStr))
            LevelFinish();


    }

    private void LevelFinish()
    {
        isLevelFinish = true;
        tweenPosLeft.Play(PlayDirection.Forward);
        tweenPosRight.Play(PlayDirection.Forward);
        tweenAlpha.Play(PlayDirection.Forward);
        tweenAlpha.onFinished.AddListener(() => {
            gameLevelItem.TriggerResult(true);
        });
    }



}
